package com.thinkgem.jeesite.modules.sys.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.SqlCondition;
import com.thinkgem.jeesite.common.utils.StringUtils;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * @Author:Kongxiangsheng
 * @DesCription:
 * @Date:Created in 6:28 2018/8/14
 * @Modified By:
 */
@Data
public class SysLog implements Serializable {

    public static final String COL_ID = "`id`";
    public static final String COL_TYPE = "`type`";
    public static final String COL_TITLE = "`title`";
    public static final String COL_CREATE_BY = "`create_by`";
    public static final String COL_CREATE_DATE = "`create_date`";
    public static final String COL_REMOTE_ADDR = "`remote_addr`";
    public static final String COL_USER_AGENT = "`user_agent`";
    public static final String COL_REQUEST_URI = "`request_uri`";
    public static final String COL_METHOD = "`method`";
    public static final String COL_PARAMS = "`params`";
    public static final String COL_EXCEPTION = "`exception`";

    /**
     * 序列化
     */
    private static final long serialVersionUID = 1L;
    /**
     * 主键
     */
    private String id;
    /**
     * 日志类型（1：接入日志；2：错误日志）
     */
    private String type;
    /**
     * 日志标题
     */
    @TableField(condition = SqlCondition.LIKE, strategy = FieldStrategy.NOT_EMPTY)
    private String title;
    /**
     * 创建人
     */
    @TableField(fill = FieldFill.INSERT, strategy = FieldStrategy.NOT_EMPTY)
    private String createBy;
    /**
     * 创建
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createDate;
    /**
     * 操作用户的IP地址
     */
    private String remoteAddr;
    /**
     * 操作用户代理信息
     */
    private String userAgent;
    /**
     * 操作的URI
     */
    @TableField(condition = SqlCondition.LIKE)
    private String requestUri;
    /**
     * 操作的方式
     */
    private String method;
    /**
     * 操作提交的数据
     */
    private String params;
    /**
     * 异常信息
     */
    private String exception;
    /**
     * 开始日期
     */
    @TableField(exist = false)
    private Date beginDate;
    /**
     * 结束日期
     */
    @TableField(exist = false)
    private Date endDate;

    /**
     * 设置请求参数
     *
     * @param paramMap
     */
    public void setParams(Map paramMap) {
        if (paramMap == null) {
            return;
        }
        StringBuilder params = new StringBuilder();
        for (Map.Entry<String, String[]> param : ((Map<String, String[]>) paramMap).entrySet()) {
            params.append(("".equals(params.toString()) ? "" : "&") + param.getKey() + "=");
            String paramValue = (param.getValue() != null && param.getValue().length > 0 ? param.getValue()[0] : "");
            params.append(StringUtils.abbr(StringUtils.endsWithIgnoreCase(param.getKey(), "password") ? "" : paramValue, 100));
        }
        this.params = params.toString();
    }
}
